package com.xueyi.aixihu.response;

import com.xueyi.aixihu.exception.SzException;
import com.xueyi.aixihu.response.base.BaseResponse;
import com.xueyi.aixihu.status.SystemCode;
import com.xueyi.aixihu.utils.AliYunOSSUtil;
import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author LuQi
 * @类描述 上传图片的Response
 * @时间 2021/8/16
 */
public class UploadImageResponse<T> extends BaseResponse<T> {


    @Getter
    private String imageUrl;

    @Getter
    private String url;


    public static <T> UploadImageResponse<T> build(String url) {
        UploadImageResponse<T> uploadImageResponse = new UploadImageResponse<T>();
        uploadImageResponse.url = url;
        if (uploadImageResponse.url == null || "权限校验失败".equals(url)) {
            uploadImageResponse.setCode(100);
            uploadImageResponse.setMsg(url);
        }
        return uploadImageResponse;
    }

    public static <T> UploadImageResponse<T> build() {
        return new UploadImageResponse<T>();
    }

    /**
     * 上传图片
     *
     * @param file
     * @param status
     * @return
     * @throws SzException
     */
    public UploadImageResponse upload(MultipartFile file, UploadImageStatus status) throws SzException {

        if (file == null || status == null) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "请检查file文件和状态");
        }

        this.imageUrl = AliYunOSSUtil.upload(file, status);
        this.url = this.imageUrl;
        return this;
    }


    /**
     * 图片上传的类型
     */
    public enum UploadImageStatus {

        /**
         * 公告图片信息
         */
        SZ_HTTP_IMAGE_NOTICE,
        /**
         * 图片或视频
         */
        SZ_IV

    }
}
